var items = new Array();
$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmh/saveCmsmh.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					cmsmhAddReset();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmh/updateCmsmh.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("修改成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	ajaxTable();
	setDialogSearch();
	closeDialogSearch();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
	if(globalMaps.get('2') == undefined) {
		$("#toolbarAdd").hide();
		$("#toolbarForbidden").hide();
		$("#toolbarStartUsing").hide();
	}
});
function ajaxTable() {
	$('#cmsmhTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				openDialogAdd();
			}
		}, '-', {
			id : 'toolbarForbidden',
			text : '禁用',
			iconCls : 'icon-cancel',
			handler : function() {
				batch('disable');
			}
		}, '-', {
			id : 'toolbarStartUsing',
			text : '启用',
			iconCls : 'icon-tip',
			handler : function() {
				batch('startUsing');
			}
		}, '-', {
			id : 'toolbarSearch',
			text : '查找',
			iconCls : 'icon-search',
			handler : function() {
				openDialogSearch();
			}
		}, '-', {
			id : 'toolbarBin',
			text : '库位管理',
			iconCls : 'icon-view',
			handler : function() {
				openDialogBin('toolbarBin');
			}
		} ],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
			mhaa : $("#searchMhaa").val(),
			mhab : $("#searchMhab").val(),
			mhad : $("#searchMhad").combobox("getValue")
		},
		onClickRow : function(rowIndex, rowData) {
			$('#cmsmhTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined) {
				openDialogUpdate(rowData.mhaa,rowData.mhab,rowData.mhac,rowData.mhad);
			}
	    }
	}).datagrid('acceptChanges');
}


function mhadFormater(value, row, index) {
	if(row.mhad == "1") { 
		return "正常";
	} else {
		return "禁用";
	}
}
function reloadTable() {
	$('#cmsmhTable').datagrid('reload');
	$('#cmsmhTable').datagrid('clearSelections');
}
function setDialogSearch() {
	$('#cmsmhSearch').dialog({
		title : '查询仓库',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogSearch() {
	$('#cmsmhSearch').dialog('open');
}
function openDialogBin(flag) {
	if ($('#cmsmhTable').datagrid('getSelected')) {
		var selectedRow = $('#cmsmhTable').datagrid('getSelections');
		var mhaa = selectedRow[0].mhaa;
		if(flag == "toolbarBin") {
			var url = '/wms/cmsmi/cmsmiMain.xhtml?mhaa=' + mhaa ;		
			window.showModalDialog(url,"","dialogWidth=900px; dialogHeight=700px; help=no; status=no; scroll=yes; resizable=no;");
		}
	}
}
function closeDialogSearch() {
	$("#searchMhaa").val("");
	$("#searchMhab").val("");
	$("#searchMhad").combobox("select", "");
	$('#cmsmhSearch').dialog('close');
}
function searchCmsmh() {
	var queryParams = $('#cmsmhTable').datagrid('options').queryParams;
	queryParams.mhaa = $("#searchMhaa").val();
	queryParams.mhab = $("#searchMhab").val();
	queryParams.mhad = $("#searchMhad").combobox("getValue");
	reloadTable();
	closeDialogSearch();
}
function setDialogAdd() {
	$('#cmsmhAdd').dialog({
		title : '新增仓库',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	cmsmhAddReset();
	$('#cmsmhAdd').dialog('open');
}
function closeDialogAdd() {
	$('#cmsmhAdd').dialog('close');
}
function cmsmhAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function cmsmhAddReset() {
	$("#cmsmhAddMhaa").val("");
	$("#cmsmhAddMhab").val("");
	$("#cmsmhAddMhac").val("");
	$("#cmsmhAddMhad").combobox("select", "");
}
function setDialogUpdate() {
	$('#cmsmhUpdate').dialog({
		title : '修改仓库',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate(mhaa, mhab, mhac, mhad) {
	items[0] = mhaa;
	items[1] = mhab;
	items[2] = mhac;
	items[3] = mhad;
	cmsmhUpdateReset(mhaa, mhab, mhac, mhad);
	$('#cmsmhUpdate').dialog('open');
}
function closeDialogUpdate() {
	$('#cmsmhUpdate').dialog('close');
}
function cmsmhUpdateReset(mhaa, mhab, mhac, mhad) {
	$("#cmsmhInfoUpdateMhaa").val(mhaa);
	$("#cmsmhUpdateMhaa").text(mhaa);
	$("#cmsmhUpdateMhab").val(mhab);
	$("#cmsmhUpdateMhac").val(mhac);
	if(mhad == 1) {
		$("#cmsmhUpdateMhad").text("正常");
	} else {
		$("#cmsmhUpdateMhad").text("禁用");
	}
}
function cmsmhEdit() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function batch(flag) {
	if ($('#cmsmhTable').datagrid('getSelected')) {
		var ids = [];
		var cods = [];
		var selectedRow = $('#cmsmhTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mhaa);
			cods.push(selectedRow[i].mhab);
		}
		var mhaa = ids.join(',');
		if(flag == "disable") {
			$.messager.confirm('提示', '你确定禁用所选仓库吗?<br/>' + cods.join(','), function(r) {
				if(r){
					changeStatus('/wms/cmsmh/disableCmsmh.xhtml?mhaa=' + mhaa);
				}
			});
		} else if(flag == "startUsing") {
			$.messager.confirm('启用提示', '你确定启用下列单据类别吗?<br/>' + cods.join(','), function(r) {
				if(r) {
					changeStatus('/wms/cmsmh/startUsingCmsmh.xhtml?mhaa=' + mhaa);
				}
			});
		}
	}
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}